﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Galaktika.BI.Runtime;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.Intermediate;

namespace Galaktika.BI.Silverlight.Controls
{
    public partial class ImageControl : ContentControl, IBindableObject, IEditableObject
    {
        public ImageControl()
        {
        }

        #region Data Properties

        public object AActionInvoker { get; set; }

        #endregion Data Properties

        #region Appearance Properties

        public byte[] AImage { get; set; }

        public Image Picture { get; set; }

        public ContentAlignment APictureAlignment { get; set; }

        public PictureSizeMode ASizeMode { get; set; }

        public BorderStyles AButtonStyle { get; set; }

        public bool AShowToolTips { get; set; }

        public string AToolTipText { get; set; }

        public Cursor ACursor
        {
            get
            {
                return base.Cursor;
            }
            set
            {
                base.Cursor = value;
            }
        }

        #endregion Appearance Properties

        #region IBindableObject Members

        public IBindingContext Context { get; set; }

        private IDictionary<string, IBindingManager> m_Bindings;
        public System.Collections.Generic.IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new SafeDictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }

        #endregion

        #region IEditableObject Members

        public void BeginEdit()
        {

        }

        public void CancelEdit()
        {

        }

        public void EndEdit()
        {
            var bi = new BitmapImage();
            bi.SetSource(new MemoryStream(AImage));
            this.Picture = new Image()
            {
                Source = bi,
                Stretch = Stretch.None
            };

            switch (this.APictureAlignment)
            {
                case ContentAlignment.TopLeft:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                    break;
                case ContentAlignment.TopCenter:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    break;
                case ContentAlignment.TopRight:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                    break;
                case ContentAlignment.MiddleLeft:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                    break;
                case ContentAlignment.MiddleCenter:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    break;
                case ContentAlignment.MiddleRight:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                    break;
                case ContentAlignment.BottomLeft:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Bottom;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
                    break;
                case ContentAlignment.BottomCenter:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Bottom;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    break;
                case ContentAlignment.BottomRight:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Bottom;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                    break;
                default:
                    base.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                    base.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                    break;
            }

            switch (this.ASizeMode)
            {
                case PictureSizeMode.Clip:
                    this.Picture.Stretch = Stretch.None;
                    break;

                case PictureSizeMode.Stretch:
                    this.Picture.Stretch = Stretch.Fill;
                    break;
                case PictureSizeMode.Zoom:
                    this.Picture.Stretch = Stretch.Uniform;
                    break;
                default:
                    this.Picture.Stretch = Stretch.None;
                    break;
            }


            base.Content = Picture;


            if (this.AShowToolTips)
            {
                ToolTipService.SetToolTip(this, new StyledToolTip() { Content = this.AToolTipText });
            }

            switch (this.AButtonStyle)
            {
                case BorderStyles.HotFlat:
                case BorderStyles.Flat:
                case BorderStyles.NoBorder:
                    base.BorderThickness = new Thickness(0.0);
                    break;
                default:
                    break;
            }
        }

        #endregion

    }
}
